package impl;

import anaydis.sort.Sorter;
import anaydis.sort.SorterType;
import anaydis.sort.gui.ObservableSorter;
import anaydis.sort.gui.SorterListener;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


public abstract class AbstractSorter implements Sorter, ObservableSorter {
    private List<SorterListener> listeners;
    private SorterType sorterType;

    protected AbstractSorter(SorterType sorterType){
        listeners = new ArrayList<SorterListener>();
        this.sorterType = sorterType;
    }

    public <T> void sort(Comparator<T> tComparator,  List<T> ts) {
    }

    public SorterType getType() {
        return sorterType;
    }

    protected <T> boolean greater(Comparator<T> c, List<T> list, int i, int j) {
        for(SorterListener sorterListener : listeners){
            sorterListener.greater(i,j);
        }
        return c.compare(list.get(i), list.get(j)) > 0;
    }

    protected <T> boolean equals(Comparator<T> c, List<T> list, int i, int j) {
        for(SorterListener sorterListener : listeners){
            sorterListener.equals(i,j);
        }
        return c.compare(list.get(i), list.get(j)) == 0;
    }

    protected <T> void swap(List<T> list, int i, int j) {
        T aux = list.set(i, list.get(j));
        list.set(j,aux);
        for(SorterListener sorterListener : listeners){
            sorterListener.swap(i,j);
        }
    }

    public void addSorterListener(@NotNull SorterListener sorterListener) {
        listeners.add(sorterListener);
    }

    public void removeSorterListener(@NotNull SorterListener sorterListener) {
        listeners.remove(sorterListener);
    }
}
